const app = getApp()
Page({
  data: {
    listDs: [],
    gjz: ''
  },
  onLoad: function(options) {
    const that = this
    wx.cloud.database({
      env: 'gtss-2b1812'
    }).collection('Teacher').field({
      _id: true,
      teaName: true,
      teaPhone: true,
      teaPost: true
    }).limit(20).where({
      teaDept: app.globalData.userDept,
      teaState: 0
    }).get().then(res => {
      that.setData({
        listDs: res.data
      })
    })
  },
  onReachBottom: function() {
    const that = this
    const db = wx.cloud.database({
      env: 'gtss-2b1812'
    })
    if (that.data.gjz == '') {
      db.collection('Teacher').field({
        _id: true,
        teaName: true,
        teaPhone: true,
        teaPost: true
      }).limit(20).skip(that.data.listDs.length).where({
        teaDept: app.globalData.userDept,
        teaState: 0
      }).get().then(res => {
        that.setData({
          listDs: that.data.listDs.concat(res.data)
        })
      })
    }
    if (that.data.gjz != '') {
      db.collection('Teacher').field({
        _id: true,
        teaName: true,
        teaPhone: true,
        teaPost: true
      }).limit(20).skip(that.data.listDs.length).where({
        teaDept: app.globalData.userDept,
        teaState: 0,
        teaName: new db.RegExp({
          regexp: that.data.gjz,
          options: 'i',
        })
      }).get().then(res => {
        that.setData({
          listDs: that.data.listDs.concat(res.data)
        })
      })
    }
  },
  funDsxq: function(e) {
    wx.cloud.database({
      env: 'gtss-2b1812'
    }).collection('Topic').where({
      teaId: e.currentTarget.dataset.id,
      ktzt: 0
    }).count().then(res => {
      if (res.total > 0) {
        wx.navigateTo({
          url: "../stuDskt/stuDskt?_id=" + e.currentTarget.dataset.id + '&from=stuDs'
        })
      } else {
        wx.showToast({
          title: '该导师暂无可选课题！',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  inpGjz: function(e) {
    const that = this
    that.setData({
      gjz: e.detail.value
    })
    if (e.detail.value == '') {
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Teacher').field({
        _id: true,
        teaName: true,
        teaPhone: true,
        teaPost: true
      }).limit(20).where({
        teaDept: app.globalData.userDept,
        teaState: 0
      }).get().then(res => {
        that.setData({
          listDs: res.data
        })
      })
    }
  },
  search: function() {
    const that = this
    const db = wx.cloud.database({
      env: 'gtss-2b1812'
    })
    db.collection('Teacher').where({
      teaDept: app.globalData.userDept,
      teaState: 0,
      teaName: new db.RegExp({
        regexp: that.data.gjz,
        options: 'i',
      })
    }).field({
      _id: true,
      teaName: true,
      teaPhone: true,
      teaPost: true
    }).limit(20).get().then(res => {
      that.setData({
        listDs: res.data
      })
    })
  }
})